<template>
  <view class="login-page" :style="my_style">
    <view class="flex-center u-m-b-60">
      <image class="w-160 h-160 radius-20" :src="logo"></image>
    </view>
    <!-- 表单 -->
    <view class="u-m-t-24 form">
      <view class="cell flex">
        <view class="w-144">手机号</view>
        <input type="number" maxlength="11" v-model="mobile" placeholder="请输入手机号" />
      </view>
      <view class="cell flex u-m-b-20">
        <view class="w-144">验证码</view>
        <input type="number" maxlength="6" v-model="verify_code" placeholder="请输入短信验证码" />
        <view class="main-color u-flex-1 u-text-right" @tap="$u.debounce(showCode, 2000, true)">
          {{ btnCodeText }}
        </view>
      </view>
      <view class="light-color font-24 lh-34 u-m-b-30 u-m-t-20"> *首次登录即视为注册</view>
      <u-button
        :disabled="disabled"
        @tap="$u.debounce(login, 2000, true)"
        type="primary"
        shape="circle"
        :custom-style="custom_btn"
      >
        手机号一键注册/登录</u-button
      >
    </view>
    <view class="footer u-text-center">
      <text class="font-24 lh-34"
        >点击上方按钮，即表示同意
        <text class="main-color" @tap.stop="jump('/views/login/protocol?tag=registeragreement')"
          >《服务协议》</text
        >和
        <text class="main-color" @tap.stop="jump('/views/login/protocol?tag=privacypolicy')"
          >《隐私政策》</text
        ></text
      >
    </view>
    <!-- 图形验证码 -->
    <image-code :show.sync="show_img" :img_url="img_url" @change="imgCode" @confirm="confirmCode" />
  </view>
</template>
<script>
  import md5Libs from '@/utils/md5'
  import mixin from '@/mixin/captcha'
  import ImageCode from '@/components/common/ImageCode.vue'
  export default {
    mixins: [mixin],
    data() {
      return {
        active: 'login',
        is_agree: true,
        code: '', // 验证码
        confirm_password: '', // 确认密码
        mobile: '', // 用户手机号
        verify_code: '', // 验证码
        password: '', // 密码
        show_img: false, // 图形验证码
        img_url: '', // 图形验证码
        logo: '',
      }
    },
    components: {
      ImageCode,
    },
    onLoad(options) {
      this.copyright()
      this.imgCode()
    },
    computed: {
      disabled() {
        return !(this.mobile && this.verify_code)
      },
    },
    methods: {
      // 获取图形验证码
      showCode() {
        if (!this.mobile) {
          this.showMsg('请输入手机号码')
          return
        }
        if (!this.$u.test.mobile(this.mobile)) {
          this.showMsg('请输入正确的手机号码')
          return
        }
        this.show_img = true
      },
      // 确认输入图形验证码
      confirmCode(val) {
        this.show_img = false
        this.sendCaptcha(this.mobile, 'login', val)
      },
      async copyright() {
        const res = await this.$api.common.system('copyright')
        if (res.code === 200) {
          this.logo = res.data.logo
        }
      },
      /**
       * 用户登录
       */
      async login() {
        const { mobile, verify_code } = this
        let params = {
          mobile,
          verify_code,
          role_type: 1,
          nonce: this.randomStr(10),
        }
        if (!this.$u.test.mobile(this.mobile)) {
          this.showMsg('请输入正确的手机号码')
          return
        }
        if (!this.$u.test.code(verify_code, 6)) {
          this.$u.toast('请输入短信验证码')
          return
        }
        const res = await this.$api.common.user('loginWithMobileCode', params)
        if (res.code === 200) {
          const { access_token, client } = res.data
          uni.setStorageSync('access_token', access_token)
          uni.setStorageSync('member_id', client.member_id)
          uni.showToast({
            title: '登录成功',
            icon: 'success',
          })
          setTimeout(() => {
            this.back()
          }, 1000)
        }
      },
      // 返回上一页
      back() {
        // 获取页面栈
        var pages = getCurrentPages()
        if (pages.length > 1) {
          // 上一个页面实例对象
          var prePage = pages[pages.length - 2]
          if (prePage.route === 'views/share/buy') {
            // 触发上个界面
            // #ifdef H5
            prePage.mobile = this.mobile
            // #endif
            // #ifdef MP-WEIXIN||APP-PLUS
            prePage.$vm.mobile = this.mobile
            // #endif
          }
        }
        this.goBack(1)
      },
    },
  }
</script>
<style lang="scss" scoped>
  .login-page {
    position: relative;
    min-height: 100vh;
    padding: 60rpx 24rpx 40rpx;
    background: $uni-bg-color;

    .bg-box {
      width: 100%;
      height: 720rpx;
      padding: 28rpx 86rpx 40rpx;
      margin-bottom: 118rpx;
      // background: url('../static/login/img_denglu@3x.png');
      background-size: 100% 100%;
    }

    .tab-item {
      width: 50%;
      height: 100rpx;
      line-height: 100rpx;
      color: #666;
    }

    .form {
      .cell {
        height: 88rpx;
        border-bottom: 1px solid #ececec;

        input {
          width: 390rpx;
          border-bottom: none;
        }

        .code-btn {
          padding: 6rpx 16rpx;
          font-size: 20rpx;
          line-height: 28rpx;
          color: #999;
          border: 1px solid #999;
          border-radius: 4rpx;
        }
      }
    }

    .footer {
      position: absolute;
      bottom: 40rpx;
      width: 100%;
    }
  }
</style>
